import random
import time
import multiprocessing


"""
    生成列表( 0到n*10000 ), 由慢到快:
        test1 ----> test2 -----> test3 -----> test4
    若采取异步, 则会有所出入
"""

def test1(n):
    t0 = time.time()
    # time.sleep(random.random())
    lst = []
    for i in range(n*10000):
        lst = lst + [i]

    t1 = time.time()
    print("test1-----%.4f"%(t1-t0))

    return lst

def test2(n):
    t0 = time.time()
    # time.sleep(random.random())
    lst = []
    for i in range(n*10000):
        lst.append(i)

    t1 = time.time()
    #print("test2", t1-t0)
    print("test2-----%.4f"%(t1-t0))

    return lst

def test3(n):
    t0 = time.time()
    # time.sleep(random.random())
    lst = []
    lst = [i for i in range(n*9999)]

    t1 = time.time()
    #print("test3", t1-t0)
    print("test3-----%.4f"%(t1-t0))

    return lst

def test4(n):
    t0 = time.time()
    # time.sleep(random.random())

    lst = []
    lst = list(range(n*10000))

    t1 = time.time()
    #print("test4", t1-t0)

    print("test4-----%.4f"%(t1-t0))
    return lst 

# print("-----start-----")
num = int(input("test num: "))
# t0 = time.time()
# test1(num)
# t1 = time.time()
# test2(num)
# t2 = time.time()
# test3(num)
# t3 = time.time()
# test4(num)
# t4 = time.time()
#
# print("------end-------")
#
#
#
# pool = multiprocessing.Pool(processes=4)
# # pool.apply_async(test1, num)
# # pool.apply_async(test2, num)
# # pool.apply_async(test3, num)
# # pool.apply_async(test4, num)
# for i in range(1,5):
#     pool.apply_async(globals().get('test%d'%i), [num])
#
# pool.close()
# pool.join()


print("-"*50)

print("开始真正的测试时间")

from timeit import Timer

timer1 = Timer("test1(%d)" % num, "from __main__ import test1")
print("concat ", timer1.timeit(number=3), "seconds=1000", "seconds")

timer2 = Timer("test2(%d)" % num, "from __main__ import test2")
print("append ", timer2.timeit(number=3), "seconds=1000", "seconds")

timer3 = Timer("test3(%d)" % num, "from __main__ import test3")
print("comprehension ", timer3.timeit(number=3), "seconds=1000", "seconds")

timer4 = Timer("test4(%d)" % num, "from __main__ import test4")
print("list range ", timer4.timeit(number=3), "seconds=1000", "seconds")

"""
 timeit模块可以用来测试一小段Python代码的执行速度
 number表示测试次数
"""



